//
//  Copyright (C) 2025 Google, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import <UIKit/UIKit.h>
#import <GoogleMobileAds/GoogleMobileAds.h>

@interface BannerSnippets : UIViewController

@property(nonatomic, strong) GADBannerView *bannerView;

@end

@implementation BannerSnippets

- (void)viewDidLoad {
  [super viewDidLoad];
  [self setUpBannerViewProgrammatically];
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  [self loadInlineAdaptiveBanner];
}

// [START handle_orientation_changes]
- (void)viewWillTransitionToSize:(CGSize)size
       withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    // Load a new ad for the new orientation.
  } completion:nil];
}
// [END handle_orientation_changes]

- (void)setUpBannerViewProgrammatically {
  // [START create_admob_banner_view]
  // Initialize the banner view.
  GADBannerView *bannerView = [[GADBannerView alloc] init];
  UIView *view = self.view;

  bannerView.translatesAutoresizingMaskIntoConstraints = NO;
  [view addSubview:bannerView];

  // This example doesn't give width or height constraints, as the ad size gives the banner an
  // intrinsic content size to size the view.
  [NSLayoutConstraint activateConstraints:@[
    // Align the banner's bottom edge with the safe area's bottom edge
    [bannerView.bottomAnchor
        constraintEqualToAnchor:view.safeAreaLayoutGuide.bottomAnchor],
    // Center the banner horizontally in the view
    [bannerView.centerXAnchor constraintEqualToAnchor:view.centerXAnchor],
  ]];

  self.bannerView = bannerView;
  // [END create_admob_banner_view]
}

- (void)loadInlineAdaptiveBanner {
  GADBannerView *bannerView = self.bannerView;
  UIView *view = self.view;

  // [START get_width]
  CGFloat totalWidth = CGRectGetWidth(view.bounds);
  // Make sure the ad fits inside the readable area.
  UIEdgeInsets insets = view.safeAreaInsets;
  CGFloat adWidth = totalWidth - insets.left - insets.right;
  // [END get_width]

  // View is not laid out yet, return early.
  if (adWidth <= 0) {
    return;
  }

  // [START set_adaptive_ad_size]
  GADAdSize adSize = GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(adWidth);
  bannerView.adSize = adSize;
  // [END set_adaptive_ad_size]

  // Test ad unit ID for inline adaptive banners.
  bannerView.adUnitID = @"ca-app-pub-3940256099942544/2435281174";
  [bannerView loadRequest:[GADRequest request]];
}

@end
